package main

import (
	"go_practice/structs"
	"math"
)

type TreeNode = structs.TreeNode

// bst 中序有序
// 相邻两个元素之差最小值

func minDiffInBST(root *TreeNode) int {
	var pre int = -1
	var min = math.MaxInt64
	var inorder func(node *TreeNode)
	inorder = func(node *TreeNode) {
		if node == nil {
			return
		}
		inorder(node.Left)

		//
		if pre != -1 && node.Val-pre < min {
			min = node.Val - pre
		}
		pre = node.Val

		inorder(node.Right)
	}

	inorder(root)
	return min
}
